//@author A0101906R
#include "CmdView.h"
#include <sstream>

using namespace Twentyfifth;
using namespace std;

const std::string CmdView::FEEDBACK_VIEWOK = "Displaying tasks.\n";
const std::string CmdView::ERROR_UNDOVIEW = "View cannot be undone.\n";
//@author A0101906R
CmdView::CmdView() {
	cmdType = VIEW;
}
//@author A0101906R
Status CmdView::execution() {

	Status viewStatus;
	int id;
	assert(storage != NULL);
	id = task.getId();
	if (id != 0) {
		viewStatus = storage->viewTask(id);
		setDescForViewID(viewStatus);
	} else {
		viewStatus = storage->viewTasks();
		setDescForViewAll(viewStatus);
	}

	return viewStatus;
}
//@author A0101906R
Status CmdView::undo(Status::StatusCode c) {
	return Status(Status::ERR_OTHER, ERROR_UNDOVIEW);
}
//@author A0101906R
void CmdView::setDescForViewAll(Status& s) {

	if (s.code == Status::OK) {
		s.desc = FEEDBACK_VIEWOK;
	} else {
		s.desc = statusCodeToString(s);
	}
	return;
}
//@author A0101906R
void CmdView::setDescForViewID(Status& s) {
	if (s.code == Status::OK) {
		assert(s.tasks.size() != 0);
		s.desc = FEEDBACK_VIEWOK + s.tasks[0].toString();
		s.tasks.clear();
	} else {
		s.desc = statusCodeToString(s);
	}
	return;
}
//@author A0101906R
void CmdView::descToString(Status& s) {

	int i;

	for (i = 0; i < s.tasks.size(); i++) {
		ostringstream convert;
		string ids;
		convert << s.tasks[i].getId() << "\t";
		ids = convert.str();
		s.desc = s.desc + "ID: " + ids + s.tasks[i].getDescription() + "\n";
	}
	s.tasks.clear();
}

